/**
 * Copyright 2016 JustWayward Team
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ozy.kxreader.mvp.ui.widget

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.OrientationHelper
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.Gravity
import android.view.View

import com.chad.library.adapter.base.BaseQuickAdapter
import com.ozy.kxreader.utils.LogUtils


class SpaceDecoration : RecyclerView.ItemDecoration {

    private var halfSpace: Int = 0
    private val headerCount = -1
    private val footerCount = Integer.MAX_VALUE
    private var mPaddingEdgeSide = true
    private var mPaddingStart = true
    private var mPaddingHeaderFooter = false
    private var mPaddingEnd = true

    private var mColorDrawable = ColorDrawable(Color.parseColor("#DDDCD7"))


    constructor(space: Int) {
        this.halfSpace = space / 2
    }

    constructor(space: Int, color: Int, paddingStart: Boolean, paddingEnd: Boolean, paddingEdgeSide: Boolean) {
        this.halfSpace = space / 2
        this.mColorDrawable = ColorDrawable(color)
        this.setPaddingStart(paddingStart)
        this.setPaddingEnd(paddingEnd)
        this.setPaddingEdgeSide(paddingEdgeSide)
    }


    /**
     * 边缘线
     *
     * @param mPaddingEdgeSide
     */
    fun setPaddingEdgeSide(mPaddingEdgeSide: Boolean) {
        this.mPaddingEdgeSide = mPaddingEdgeSide
    }

    /**
     * 第一一条线
     *
     * @param mPaddingStart
     */
    fun setPaddingStart(mPaddingStart: Boolean) {
        this.mPaddingStart = mPaddingStart
    }

    /**
     * 最后一条线
     *
     * @param mPaddingEnd
     */
    fun setPaddingEnd(mPaddingEnd: Boolean) {
        this.mPaddingEnd = mPaddingEnd
    }

    /**
     * 头部和尾部的线
     *
     * @param mPaddingHeaderFooter
     */
    fun setPaddingHeaderFooter(mPaddingHeaderFooter: Boolean) {
        this.mPaddingHeaderFooter = mPaddingHeaderFooter
    }


    fun setColor(color: Int) {
        mColorDrawable = ColorDrawable(color)
    }


    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
        val position = parent.getChildAdapterPosition(view)
        var spanCount = 0
        var orientation = 0
        var spanIndex = 0
        var headerCount = 0
        var footerCount = 0
        var count = parent.childCount
        var isFull = false
        if (parent.adapter is BaseQuickAdapter<*, *>) {
            headerCount = (parent.adapter as BaseQuickAdapter<*, *>).headerLayoutCount
            footerCount = (parent.adapter as BaseQuickAdapter<*, *>).footerLayoutCount
            count = count - headerCount - footerCount
        }

        val layoutManager = parent.layoutManager
        if (layoutManager is StaggeredGridLayoutManager) {
            orientation = layoutManager.orientation
            spanCount = layoutManager.spanCount
            spanIndex = (view.layoutParams as StaggeredGridLayoutManager.LayoutParams).spanIndex
            isFull = (view.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan
        } else if (layoutManager is GridLayoutManager) {
            orientation = layoutManager.orientation
            spanCount = layoutManager.spanCount
            spanIndex = (view.layoutParams as GridLayoutManager.LayoutParams).spanIndex
            isFull = false
        } else if (layoutManager is LinearLayoutManager) {
            orientation = layoutManager.orientation
            spanCount = 1
            spanIndex = 0
            isFull = true
        }

        /**
         * 普通Item的尺寸
         */
        if (position >= headerCount && position < parent.adapter.itemCount - footerCount) {
            val gravity: Int
            if (isFull && spanIndex == 0)
                gravity = Gravity.FILL_HORIZONTAL
            else if (spanIndex == 0 && spanCount > 1)
                gravity = Gravity.LEFT
            else if (spanIndex == spanCount - 1 && spanCount > 1)
                gravity = Gravity.RIGHT
            else if (spanCount == 1)
                gravity = Gravity.FILL_HORIZONTAL
            else {
                gravity = Gravity.CENTER
            }
            LogUtils.warnInfo(gravity.toString() + "=====>>>>" + spanIndex)
            if (orientation == OrientationHelper.VERTICAL) {
                when (gravity) {
                    Gravity.LEFT -> {
                        if (mPaddingEdgeSide)
                            outRect.left = halfSpace * 2
                        outRect.right = halfSpace
                    }
                    Gravity.RIGHT -> {
                        outRect.left = halfSpace
                        if (mPaddingEdgeSide)
                            outRect.right = halfSpace * 2
                    }
                    Gravity.FILL_HORIZONTAL -> {
                        if (mPaddingEdgeSide) {
                            outRect.left = halfSpace * 2
                            outRect.right = halfSpace * 2
                        }
                    }
                    Gravity.CENTER -> {
                        outRect.left = halfSpace
                        outRect.right = halfSpace
                    }
                }
                if (position - headerCount < spanCount && mPaddingStart)
                    outRect.top = halfSpace * 2

                if (mPaddingEnd || position - headerCount < count - spanCount)
                    outRect.bottom = halfSpace * 2
            } else {
                when (gravity) {
                    Gravity.LEFT -> {
                        if (mPaddingEdgeSide)
                            outRect.bottom = halfSpace * 2
                        outRect.top = halfSpace
                    }
                    Gravity.RIGHT -> {
                        outRect.bottom = halfSpace
                        if (mPaddingEdgeSide)
                            outRect.top = halfSpace * 2
                    }
                    Gravity.FILL_HORIZONTAL -> if (mPaddingEdgeSide) {
                        outRect.left = halfSpace * 2
                        outRect.right = halfSpace * 2
                    }
                    Gravity.CENTER -> {
                        outRect.bottom = halfSpace
                        outRect.top = halfSpace
                    }
                }
                if (position - headerCount < spanCount && mPaddingStart)
                    outRect.left = halfSpace * 2
                outRect.right = halfSpace * 2
            }
        } else {//只有HeaderFooter进到这里
            if (mPaddingHeaderFooter) {//并且需要padding Header&Footer
                if (orientation == OrientationHelper.VERTICAL) {
                    if (mPaddingEdgeSide) {
                        outRect.left = halfSpace * 2
                        outRect.right = halfSpace * 2
                    }
                    outRect.top = halfSpace * 2
                } else {
                    if (mPaddingEdgeSide) {
                        outRect.top = halfSpace * 2
                        outRect.bottom = halfSpace * 2
                    }
                    outRect.left = halfSpace * 2
                }
            }
        }
    }


    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State?) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            //垂直
            var left = child.right
            var top = child.top - halfSpace * 2
            var right = left + halfSpace * 2
            var bottom = child.bottom + halfSpace * 2
            mColorDrawable.setBounds(left, top, right, bottom)
            mColorDrawable.draw(c)
            left = child.left

            top = child.bottom

            right = child.right

            bottom = top + halfSpace * 2

            mColorDrawable.setBounds(left, top, right, bottom)
            mColorDrawable.draw(c)
        }
    }
}